#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int N = 0;
    while(true){
        char a[5][5]={0};
        string in;
        int bi = 0, bj = 0;
        bool newline = false;
        for (int i = 0; i < 5; ++i) {
            getline(cin, in);
            if(in.size()==4) in.push_back(' ');
            if (in[0] == 'Z') return 0;
            else if(N!=0) newline = true;
            for (int j = 0; j < 5; ++j) {
                a[i][j] = in[j];
                if (a[i][j] == ' ') {
                    bi = i;
                    bj = j;
                }
            }
        }
        if(newline) cout << endl;
        bool flg = false;
        char ch;
        while(cin >> ch && ch!= '0'){
            switch (ch) {
                case 'A': {
                    if (bi == 0) {
                        flg = true;
                        break;
                    }
                    swap(a[bi][bj], a[bi-1][bj]);
                    --bi;
                    break;
                }
                case 'B': {
                    if (bi == 4) {
                        flg = true;
                        break;
                    }
                    swap(a[bi][bj], a[bi+1][bj]);
                    ++bi;
                    break;
                }
                case 'L': {
                    if (bj == 0) {
                        flg = true;
                        break;
                    }
                    swap(a[bi][bj], a[bi][bj-1]);
                    --bj;
                    break;
                }
                case 'R': {
                    if (bj == 4) {
                        flg = true;
                        break;
                    }
                    swap(a[bi][bj], a[bi][bj+1]);
                    ++bj;
                    break;
                }
                default:
                    break;
            }
        }
        cout << "Puzzle #" << ++N << ":" << endl;
        if (flg)cout << "This puzzle has no final configuration." << endl;
        else
            for (int i = 0; i < 5; ++i) {
                for (int j = 0; j < 4; ++j) cout << a[i][j] << " ";
                cout << a[i][4] << endl;
            }
        getchar();
    }
}
/*
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z

 */